export default function useTap() {
  const vTap = {
    mounted(el, binding) {
      const maxTime = 250, maxDistance = 30
      let startTime = 0, [startX, startY, endX, endY] = [0, 0, 0, 0]
      el.addEventListener('touchstart', function (e) {
        startTime = Date.now()
        startX = e.changedTouches[0].clientX
        startY = e.changedTouches[0].clientY
      })
      el.addEventListener('touchend', function (e) {
        endX = e.changedTouches[0].clientX
        endY = e.changedTouches[0].clientY
        if ((Date.now() - startTime) > maxTime || (Math.abs(endX - startX) > maxDistance || Math.abs(endY - startY) > maxDistance)) {
          return;
        }
        e.preventDefault()
        if (el.getAttribute('disabled') !== null) return
        binding.value(e);
      })
    }
  }
  return vTap
}